package cn.common.service;

import java.util.List; 

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

import cn.common.log.GeneralLog;
import cn.ziroom.basic.domain.*;
import cn.ziroom.contract.data.ConfigData;
import cn.ziroom.contract.data.ContractData;
import cn.ziroom.contract.data.CustomerData;
import cn.ziroom.contract.data.HouseData;
import cn.ziroom.contract.data.PayData;
import cn.ziroom.contract.data.RoomData;
import cn.ziroom.contract.data.RoomPicturesData;
import cn.ziroom.contract.data.VoucherOrder;
import cn.ziroom.system.domain.Message;
import cn.ziroom.system.service.MessageService;

import cn.ziroom.system.domain.User;

import com.google.gson.Gson;

/**
 * 环绕通知，用来做数据验证及记录日志
 * 
 * @author Administrator
 * 
 */
public class WebServiceMedthodInterceptor implements MethodInterceptor {

	private MessageService messageService;

	public Object invoke(MethodInvocation methodInvocation) throws Throwable {

		Message message = new Message();

		try {
			// 记录日志，将日志记录到日志文件中去
			Object[] o = methodInvocation.getArguments();
			Gson gson = new Gson();
			String json = gson.toJson(o);

			if (o != null && o.length > 0) {
				if( o[0] instanceof ContractData){		//如果是合同的信息的接口
					ContractData contract = (ContractData)o[0];
					message.setContractCode(contract.getContractCode());
					message.setFunctionName(methodInvocation.getMethod().getName());
					message.setStatus(Message.REQUEST);
					message.setJson(json);
					
					messageService.insert(message);
					
				}else if(o[0] instanceof HouseData){	//同步房屋的信息
					HouseData house = (HouseData)o[0];
					message.setHouseCode(house.getHouseCode());
					message.setFunctionName(methodInvocation.getMethod().getName());
					message.setStatus(Message.REQUEST);
					message.setJson(json);
					
					messageService.insert(message);

				}else if(o[0] instanceof RoomData){		//同步房间的接口信息
					RoomData room = (RoomData)o[0];
					
					message.setHouseCode(room.getHouseCode());
					message.setRoomCode(room.getRoomCode());
					message.setFunctionName(methodInvocation.getMethod().getName());
					message.setStatus(Message.REQUEST);
					message.setJson(json);
					
					messageService.insert(message);

				}else if(o[0] instanceof CustomerData){	//同步客户的接口
					CustomerData customer = (CustomerData)o[0];
					
					message.setCustomerCode(customer.getCustCode());
					message.setFunctionName(methodInvocation.getMethod().getName());
					message.setStatus(Message.REQUEST);
					message.setJson(json);
					
					messageService.insert(message);

				}else if(o[0] instanceof ConfigData){	//同步
					ConfigData config = (ConfigData)o[0];
					
					message.setHouseCode(config.getHouseCode());
					message.setRoomCode(config.getRoomCode());
					message.setFunctionName(methodInvocation.getMethod().getName());
					message.setStatus(Message.REQUEST);
					message.setJson(json);
					
					messageService.insert(message);

				}else if(o[0] instanceof RoomPicturesData){
					RoomPicturesData pic = (RoomPicturesData)o[0];
					
					message.setHouseCode(pic.getHouseCode());
					message.setRoomCode(pic.getRoomCode());
					message.setFunctionName(methodInvocation.getMethod().getName());
					message.setStatus(Message.REQUEST);
					message.setJson(json);
					
					messageService.insert(message);

				}else if(o[0] instanceof PayData) {
					message.setFunctionName(methodInvocation.getMethod().getName());
					message.setStatus(Message.REQUEST);
					message.setJson(json);
					
					messageService.insert(message);
				}else if(o[0] instanceof VoucherOrder) {
					message.setFunctionName(methodInvocation.getMethod().getName());
					message.setStatus(Message.REQUEST);
					message.setJson(json);
					
					messageService.insert(message);
				}else if(o[0] instanceof List){
					
					@SuppressWarnings("rawtypes")
					List list = (List)o[0];
					if(list != null && list.size() > 0){
						if(list.get(0) instanceof User){
							message.setFunctionName(methodInvocation.getMethod().getName());
							message.setStatus(Message.REQUEST);
							message.setJson(json);
							
							messageService.insert(message);
						}else if(list.get(0) instanceof City){
							message.setFunctionName(methodInvocation.getMethod().getName());
							message.setStatus(Message.REQUEST);
							message.setJson(json);
							
							messageService.insert(message);
							
						}else if(list.get(0) instanceof District){
							message.setFunctionName(methodInvocation.getMethod().getName());
							message.setStatus(Message.REQUEST);
							message.setJson(json);
							
							messageService.insert(message);
							
						}else if(list.get(0) instanceof Business){
							message.setFunctionName(methodInvocation.getMethod().getName());
							message.setStatus(Message.REQUEST);
							message.setJson(json);
							
							messageService.insert(message);
							
						}else if(list.get(0) instanceof Province){
							message.setFunctionName(methodInvocation.getMethod().getName());
							message.setStatus(Message.REQUEST);
							message.setJson(json);
							
							messageService.insert(message);
							
						}else if(list.get(0) instanceof Subway){
							message.setFunctionName(methodInvocation.getMethod().getName());
							message.setStatus(Message.REQUEST);
							message.setJson(json);
							
							messageService.insert(message);
							
						}else if(list.get(0) instanceof SubwayStation){
							message.setFunctionName(methodInvocation.getMethod().getName());
							message.setStatus(Message.REQUEST);
							message.setJson(json);
							
							messageService.insert(message);
							
						}else if(list.get(0) instanceof Cycle){
							message.setFunctionName(methodInvocation.getMethod().getName());
							message.setStatus(Message.REQUEST);
							message.setJson(json);
							
							messageService.insert(message);
							
						}else if(list.get(0) instanceof CycleFace){
							message.setFunctionName(methodInvocation.getMethod().getName());
							message.setStatus(Message.REQUEST);
							message.setJson(json);
							
							messageService.insert(message);
							
						}else if(list.get(0) instanceof Area){
							message.setFunctionName(methodInvocation.getMethod().getName());
							message.setStatus(Message.REQUEST);
							message.setJson(json);
							
							messageService.insert(message);
						}else{
							//如果都不满足的情况下，就记录到日志文件中。
							GeneralLog.getInstance().info(methodInvocation.getClass(), methodInvocation.getMethod().getName(), json);
						}
					}
					
				}else{ 
					//如果都不满足的情况下，就记录到日志文件中。
					GeneralLog.getInstance().info(methodInvocation.getClass(), methodInvocation.getMethod().getName(), json);
				}
			}

			// 在调用接口之前，将所有的日志信息都记录下来， 而方法调用后的日志信息分别写在相应的接口方法中。
			return methodInvocation.proceed();

		} catch (Throwable t) { // 如果在这个过程中执行异常， 需要记录下来。

			message.setStatus(Message.INNER_ERROR);
			message.setJson(t.getMessage());
			messageService.insert(message);

			throw t;
		}
	}

	public MessageService getMessageService() {
		return messageService;
	}

	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}
}
